package com.uphone.kingmall.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.adapter.MyTeamAdapter;
import com.uphone.kingmall.base.BaseListFragment;
import com.uphone.kingmall.bean.UserGroupsBean;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.GsonUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.SharedPreferencesHelper;

import java.util.List;

/**
 * author : SuMeng
 * e-mail : 986335838@qq.com
 * time   : 2019/05/21
 * desc   : xxxx 描述
 */
public class MyTeamFragment extends BaseListFragment {

    private int type;

    public static MyTeamFragment newInstance(int type) {

        Bundle args = new Bundle();
        MyTeamFragment fragment = new MyTeamFragment();
        fragment.type = type;
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected UserGroupsBean getBeans() {
        return new UserGroupsBean();
    }

    @Override
    protected HttpParams getParams() {
        HttpParams params = new HttpParams();
        params.put("userId", SharedPreferencesHelper.getUserId());
        params.put("type", type);
        return params;
    }

    @Override
    protected String getUrl() {
        return MyUrl.userGroup;
    }

    @Override
    protected void loadData(int page) {
        this.page = page;
        String url = getUrl();
        if (TextUtils.isEmpty(url)) {
            return;
        }
        OkGoUtils.normalRequest(url, getParams(), new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                if (code == 0) {
                    UserGroupsBean bean = GsonUtils.getGson().fromJson(body, UserGroupsBean.class);
                    if (bean != null) {
                        List<UserGroupsBean.UserGroupBean> dataList = bean.getUserGroup();
                        mAdapter.setNewData(dataList);
                        if (dataList == null || dataList.size() == 0) {
                            tvEmpty.setVisibility(View.VISIBLE);
                        } else {
                            tvEmpty.setVisibility(View.GONE);
                        }
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    @Override
    protected void itemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new MyTeamAdapter();
    }

    @Override
    public void initView(View view) {
        refreshLayout.setEnableLoadMore(false);
    }
}
